﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WindowsChart
{
    public partial class UserControl1 : UserControl
    {
        private DateTime _full_StartTime;
        private DateTime _full_EndTime;
        private List<LineTip> _lineTips = null;

        public DateTime Full_StartTime { get => _full_StartTime; set => _full_StartTime = value; }
        public DateTime Full_EndTime { get => _full_EndTime; set => _full_EndTime = value; }
        public List<LineTip> LineTips { get => _lineTips; set => _lineTips = value; }



        private List<Color> _colors = new List<Color>() { Color.Red, Color.Yellow, Color.Green, Color.Gold, Color.MediumPurple };

        public UserControl1()
        {
            InitializeComponent();
            Draw();
        }
        public UserControl1(DateTime fullStartTime, DateTime fullEndTime, List<LineTip> lineTips) : this()
        {
            Full_StartTime = fullStartTime;
            Full_EndTime = fullEndTime;
            LineTips = lineTips;
        }

        public void Draw()
        {
            if (_lineTips != null)
            {
                toolTipForAnyPanel.AutoPopDelay = 5000; toolTipForAnyPanel.InitialDelay = 500; toolTipForAnyPanel.ReshowDelay = 500;
                toolTipForAnyPanel.ShowAlways = true;


                for (int i = 0; i < _lineTips.Count; i++)
                {
                    int x = Calculate_LocationX(_lineTips[i]);
                    int width = Calculate_Width(_lineTips[i]);

                    Point point = MakePoint(x);
                    Size size = MakeSize(width);
                    int colorIndex = ChooseColor(i);

                    AddOnePanel(point, size, _colors[colorIndex], _lineTips[i]);
                }
            }
        }

        private void UserControl1_Resize(object sender, EventArgs e)
        {
            this.Controls.Clear();

            Draw();
        }

        #region 内部正式画法
        private void AddOnePanel(Point location, Size size, Color color, LineTip lineTip)
        {
            Panel panel = new Panel();
            panel.BackColor = color;
            panel.Location = location;
            panel.Size = size;
            toolTipForAnyPanel.SetToolTip(panel, MakeMessage(lineTip));

            panel.Click += Panel_Click;
            this.Controls.Add(panel);
        }

        private void Panel_Click(object sender, EventArgs e)
        {
            var t = (Panel)sender;
            t.BringToFront();
        }

        /// <summary>
        /// 计算起点X
        /// </summary>
        /// <param name="lineTip"></param>
        /// <returns></returns>
        private int Calculate_LocationX(LineTip lineTip)
        {
            int result = 0;
            TimeSpan ts_Full = Full_EndTime - Full_StartTime;
            TimeSpan ts_Start = lineTip.TipStartTime - Full_StartTime;

            result = Convert.ToInt32(this.Width * (ts_Start.TotalSeconds / ts_Full.TotalSeconds));

            return result;
        }
        /// <summary>
        /// 计算宽度
        /// </summary>
        /// <param name="lineTip"></param>
        /// <returns></returns>
        private int Calculate_Width(LineTip lineTip)
        {
            int result = 0;
            TimeSpan ts_Full = Full_EndTime - Full_StartTime;
            TimeSpan ts_Part = lineTip.TipEndTime - lineTip.TipStartTime;

            result = Convert.ToInt32(this.Width * (ts_Part.TotalSeconds / ts_Full.TotalSeconds));

            return result;
        }
        private Point MakePoint(int pointX,int pointY = 0)
        {
            Point result = new Point(pointX,pointY);

            return result;
        }

        private Size MakeSize(int width)
        {
            Size result = new Size(width, this.Height);
            return result;
        }

        private int ChooseColor(int index)
        {
            int result = 0;
            result = index % _colors.Count;

            return result;
        }

        private string MakeMessage(LineTip lineTip)
        {
            string result = string.Empty;
            result = $"{lineTip.TipStartTime} ~\n{lineTip.TipEndTime}\n{lineTip.Message}";
            return result;
        }

        #endregion
    }

    /// <summary>
    /// 时间线的提示：包含开始时间、结束时间、提示信息
    /// </summary>
    public class LineTip
    {
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime TipStartTime { get; set; }
        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime TipEndTime { get; set; }
        /// <summary>
        /// 提示信息
        /// </summary>
        public string Message { get; set; }
        public LineTip(DateTime tipStartTime, DateTime tipEndTime, string message)
        {
            TipStartTime = tipStartTime;
            TipEndTime = tipEndTime;
            Message = message;
        }

    }
}
